package com.yzc.android.beans;

import android.bluetooth.BluetoothDevice;
import android.os.Parcel;

/**
 * Created by yzc on 16/5/25.
 */
public class BeaconBean implements android.os.Parcelable {
    public String name;
    public int major;
    public int minor;
    public String proximityUuid;
    public String bluetoothAddress;
    public int rssi;

    public static BeaconBean fromScanData(BluetoothDevice device, int rssi, byte[] scanData) {

        int startByte = 2;
        boolean patternFound = false;
        while (startByte <= 5) {
            if ((scanData[startByte+2] & 0xff) == 0x02 &&
                    (scanData[startByte+3] & 0xff) == 0x15) {
                // yes!  This is an iBeacon
                patternFound = true;
                break;
            }
            else if ((scanData[startByte] & 0xff) == 0x2d &&
                    (scanData[startByte+1] & 0xff) == 0x24 &&
                    (scanData[startByte+2] & 0xff) == 0xbf &&
                    (scanData[startByte+3] & 0xff) == 0x16) {
                BeaconBean iBeacon = new BeaconBean();
                iBeacon.major = 0;
                iBeacon.minor = 0;
                iBeacon.proximityUuid = "00000000-0000-0000-0000-000000000000";
                return iBeacon;
            }
            else if ((scanData[startByte] & 0xff) == 0xad &&
                    (scanData[startByte+1] & 0xff) == 0x77 &&
                    (scanData[startByte+2] & 0xff) == 0x00 &&
                    (scanData[startByte+3] & 0xff) == 0xc6) {

                BeaconBean iBeacon = new BeaconBean();
                iBeacon.major = 0;
                iBeacon.minor = 0;
                iBeacon.proximityUuid = "00000000-0000-0000-0000-000000000000";
                return iBeacon;
            }
            startByte++;
        }


        if (patternFound == false) {
            // This is not an iBeacon
            return null;
        }

        BeaconBean iBeacon = new BeaconBean();
        iBeacon.rssi = rssi;

        // AirLocate:
        // 02 01 1a 1a ff 4c 00 02 15  # Apple's fixed iBeacon advertising prefix
        // e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # iBeacon profile uuid
        // 00 00 # major
        // 00 00 # minor
        // c5 # The 2's complement of the calibrated Tx Power

        // Estimote:
        // 02 01 1a 11 07 2d 24 bf 16
        // 394b31ba3f486415ab376e5c0f09457374696d6f7465426561636f6e00000000000000000000000000000000000000000000000000
        String log_major_raw = "";
        byte[] proximityUuidBytes = new byte[16];
        System.arraycopy(scanData, startByte+4, proximityUuidBytes, 0, 16);

        byte[] major_tmp = new byte[2];
        byte[] minor_tmp = new byte[2];
        System.arraycopy(scanData, startByte+20, major_tmp, 0, 2);
        System.arraycopy(scanData, startByte+22, minor_tmp, 0, 2);

        iBeacon.major = (major_tmp[0] & 0xff) * 0x100 + (major_tmp[1] & 0xff);
        iBeacon.minor = (minor_tmp[0] & 0xff) * 0x100 + (minor_tmp[1] & 0xff);

        String hexString = bytesToHexString(proximityUuidBytes);
        StringBuilder sb = new StringBuilder();
        sb.append(hexString.substring(0,8));
        sb.append("-");
        sb.append(hexString.substring(8,12));
        sb.append("-");
        sb.append(hexString.substring(12,16));
        sb.append("-");
        sb.append(hexString.substring(16,20));
        sb.append("-");
        sb.append(hexString.substring(20,32));
        iBeacon.proximityUuid = sb.toString();

        if (device != null) {
            iBeacon.bluetoothAddress = device.getAddress();
            iBeacon.name = device.getName();
        }

        return iBeacon;
    }

    private static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeInt(this.major);
        dest.writeInt(this.minor);
        dest.writeString(this.proximityUuid);
        dest.writeString(this.bluetoothAddress);
        dest.writeInt(this.rssi);
    }

    @Override
    public String toString() {
        return "BeaconBean{" + "name=" + name
                + ", proximityUuid=" + proximityUuid
                + ", major=" + major
                + ", minor=" + minor
                + ", bluetoothAddress=" + bluetoothAddress
                + ", rssi=" + rssi
                + '}';
    }


    public BeaconBean() {
    }

    protected BeaconBean(Parcel in) {
        this.name = in.readString();
        this.major = in.readInt();
        this.minor = in.readInt();
        this.proximityUuid = in.readString();
        this.bluetoothAddress = in.readString();
        this.rssi = in.readInt();
    }

    public static final Creator<BeaconBean> CREATOR = new Creator<BeaconBean>() {
        @Override
        public BeaconBean createFromParcel(Parcel source) {
            return new BeaconBean(source);
        }

        @Override
        public BeaconBean[] newArray(int size) {
            return new BeaconBean[size];
        }
    };
}
